// file_reader_test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "MyVariableSizeFileReader.h"

bool ReadFixedSizeFile(stringc& strFile);
bool ReadVariableSizeFile(stringc& strFile);

int _tmain(int argc, _TCHAR* argv[])
{
	string strFile;

	strFile = cz_file_GetExeDirA() + "..\\Data\\fixed.bin";
	if (!ReadFixedSizeFile(strFile))
	{
		cout << "ReadFixedSizeFile failed" << endl;
		getchar();
		return 0;
	}
	cout << "ReadFixedSizeFile success" << endl;

	strFile = cz_file_GetExeDirA() + "..\\Data\\variable.bin";
	if (!ReadVariableSizeFile(strFile))
	{
		cout << "ReadVariableSizeFile failed" << endl;
		cout << "please delete the file \"variable.bin.offset\" and try again" << endl;
		getchar();
		return 0;
	}
	cout << "ReadVariableSizeFile success" << endl;

	cout << "ok" << endl;
	getchar();

	return 0;
}

bool ReadFixedSizeFile(stringc& strFile)
{
	FixedSizeBinFileReader fsfr;
	u32 useMemory = 4 * 1024 * 1024;			// 4M
	u32 readFlag = READ_FLAG_FORWARD | READ_FLAG_MAPPING;
	u32 i, recordNum;
	char buf[MAX_PATH * 2 + 1];

	if (!fsfr.Init(strFile, useMemory, readFlag, MAX_PATH * 2 + 1))
	{
		CZ_LOG_ERRORA("");
		return false;
	}
	recordNum = fsfr.GetRecordNum();
	
	for (i = 0; i < recordNum; ++i)
	{
		if (!fsfr.GetRecordFromIndex(i, buf))
		{
			fsfr.Uninit();
			CZ_LOG_ERRORA("");
			return false;
		}
		//cout << buf << endl;
	}
	fsfr.Uninit();

	return true;
}

bool ReadVariableSizeFile(stringc& strFile)
{
	MyVariableSizeFileReader mvsfr;
	u32 useMemory = 2 * 1024 * 1024;			// 2M
	u32 readFlag = READ_FLAG_FORWARD | READ_FLAG_MAPPING | READ_FLAG_DELETE_TEMP_FILE;
	u32 i, recordNum;
	char buf[MAX_PATH * 2 + 1];

	if (!mvsfr.Init(strFile, useMemory, useMemory, readFlag))
	{
		CZ_LOG_ERRORA("");
		return false;
	}
	recordNum = mvsfr.GetRecordNum();

	for (i = 0; i < recordNum; ++i)
	{
		if (!mvsfr.GetRecordFromIndex(i, buf))
		{
			mvsfr.Uninit();
			CZ_LOG_ERRORA("");
			return false;
		}
		//cout << buf << endl;
	}
	mvsfr.Uninit();

	return true;
}



